<?php

namespace app\admin\controller\ykjp\sell;

use app\common\controller\Backend;

/**
 * 销售订单主管理
 *
 * @icon fa fa-circle-o
 */
class Sellfinancial extends Backend {

    /**
     * Sellfinancial模型对象
     * @var \app\admin\model\ykjp\sell\Sellfinancial
     */
    protected $model = null;
    protected $distinguish = true;

    public function _initialize() {
        parent::_initialize();
        $this->model = new \app\admin\model\ykjp\sell\Sellfinancial;
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusListList", $this->model->getStatusListList());
    }

    /**
     * 查看详情
     */
    public function detail($ids) {
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        $detail = $this->model
                ->where($where)
                ->where('firmid', $this->auth->firmid)
                ->order($sort, $order)
                ->where("id", $ids)
                ->select();
        $productModel = new \app\admin\model\ykjp\sell\Product;
        $product = $productModel->with(['products'])->where("sell_id", $ids)->select();
        $arr;
        if ($product) {
            foreach ($product as $key => $value) {
                $a = json_decode($value['products']['prop']);
                $value['products']['prop'] = $this->model->object_array($a);
                $arr[] = $value;
            }
        }
        $this->assign("detail", $detail);
        $this->assign("product", $product);
        return $this->view->fetch("detail");
    }

    /**
     * 审核
     */
    public function audit($ids = null) {

        $modelProduct = new \app\admin\model\ykjp\sell\Product;
        list($where, $sort, $order, $offset, $limit) = $this->buildparams();
        //判断通过还是驳回 （pass 通过  reject 驳回)
        if ($this->request->param("act") == "pass") {  //通过
            $data = array("status_list" => 5, "purman_id" => $this->auth->id, "updatetime" => time());
            $update = $this->model
                    ->where("status_list", 3)
                    ->where('firmid', $this->auth->firmid)
                    ->where("id", "in", $ids)
                    ->update($data);
            if ($update) {
                $this->success(null, null, array("result" => "success"));
            }
            $this->error("operation fail");
        } elseif ($this->request->param("act") == "reject") {  //驳回
            $cause = $this->request->param('cause');  //驳回原因
            $data = array("status_list" => 4, "purman_id" => $this->auth->id, "updatetime" => time(), 'cause' => $cause);
            $update = $this->model
                    ->where("status_list", 3)
                    ->where('firmid', $this->auth->firmid)
                    ->where("id", "in", $ids)
                    ->update($data);

            if ($update) {
                $this->success(null, null, array("result" => "success"));
            }
            $this->error("operation fail");
        }

        $this->error("Illegal operation");
    }

}
